import { Injectable } from '@angular/core';

import {Hero} from './hero';
import {HEROES} from './mock-heroes';

import {Observable} from 'rxjs/Observable';
import {of} from 'rxjs/observable/of';

import {MessageService} from './message.service';

// 导入http相关模块
import {HttpClient,HttpHeaders} from '@angular/common/http';

//@Injectable表示该类可以被依赖注入
@Injectable()
export class HeroService {

  private heroesUrl = "api/heroes";

  //使用private会自动生成一个private属性
  //ng提供httpclient实现http请求
  constructor(private messageService:MessageService,
  private http:HttpClient
  ) { }

  getHeroes():Observable<Hero[]>{
    this.messageService.add("helloservice:fetch heroes");
    // return of(HEROES);
    return this.http.get<Hero[]>(this.heroesUrl);
  }

  getHero(id:number):Observable<Hero>{
    this.messageService.add(`HeroService:fetched hero id=${id}`);
    return of(HEROES.find(hero => hero.id === id));
  }

  private log(message:string):void{
    this.messageService.add('HeroService:' + message);
  }

}
